package cn.yyh.org.service.impl;

import cn.yyh.basic.exception.MyException;
import cn.yyh.basic.service.impl.BaseServiceImpl;
import cn.yyh.basic.utils.FastDfsUtil;
import cn.yyh.org.domain.Employee;
import cn.yyh.org.domain.Shop;
import cn.yyh.org.domain.VO.EmployeeVO;
import cn.yyh.org.domain.VO.ShopVO;
import cn.yyh.org.mapper.EmployeeMapper;
import cn.yyh.org.mapper.ShopMapper;
import cn.yyh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: ShopServiceImpl
 * @Package: cn.yyh.org.service.impl
 * @date 2021/6/29
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<ShopMapper, Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Shop shop) {
        // 向下造型
        ShopVO vo = (ShopVO) shop;
        // 获取到其中的员工对象
        EmployeeVO admin = vo.getAdmin();
        // 如果前后两次输入的密码相同则报错
        if (!admin.getComfirmPassword().equals(admin.getPassword())) {
            throw new MyException("两次密码不一致");
        }
        // 根据前台输入的username去数据库找有没有相同的
        Employee employee = employeeMapper.getByName(admin.getUserName());
        // 如果不为null则表示数据库已经有相同的数据,则给前端个提示信息
        if (employee != null) {
            throw new MyException("账户已经存在，请直接登录！如果忘记密码，请找回密码！");
        }
        // 报错员工信息.店铺信息因为需要员工id才能添加,所以先添加员工再获取ID
        employeeMapper.save(admin);
        // 设置店铺的员工id,通过保存员工返回的id
        vo.setAdminId(admin.getId());
        // 保存店铺信息
        baseMapper.save(vo);
        // 给员工对象设置店铺id
        admin.setShopId(vo.getId());
        // 修改数据库中的员工信息,把店铺id设置进去
        employeeMapper.update(admin);
    }

}
